package com.air.ota.service.imp;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.stereotype.Service;

import com.air.ota.epay.util.NetTrans;
import com.air.ota.epay.ws.msg.SPWebService;
import com.air.ota.epay.ws.msg.SPWebServicePortType;
import com.air.ota.epay.ws.outer.MessagePacket;
import com.air.ota.mo.DepositTrans;
import com.air.ota.service.IEpayService;
import com.air.ota.util.ConfigProperties;


@Service("epayService")
public class EpayServiceImp implements IEpayService {
	
	
	
	
	@Override
	public String charge(int amount, String account,String orderId) throws Exception {
		
		SPWebService spWS = new SPWebService();
		SPWebServicePortType portType =  spWS.getSPWebServiceHttpPort();
		
		String payType = ConfigProperties.getInstance().getValue("epay.payType");
		String merchantId = ConfigProperties.getInstance().getValue("epay.merchantId");
		String busiType = ConfigProperties.getInstance().getValue("epay.busiType");
		
		SimpleDateFormat sdf  = new SimpleDateFormat("yyyyMMddHHmmss");
		Date current = new Date();
		String date=sdf.format(current); 
		
		//String orderId="0000000"+System.currentTimeMillis();
		
		
		
		//String reqData = "Amount=1000&Account=13318258474&TeleNum=13318258474&PayType=13&Date=20110531092502&MerchantId=20000050&OrderId=00000000000000005003&BusiType=1001230050";
		String reqData = "Amount="+amount+"&Account="+account+"&TeleNum="+account+"&PayType="+payType+"&Date="+date+"&MerchantId="+merchantId+"&OrderId="+orderId+"&BusiType="+busiType;
		System.out.println("发送扣费请求到统一支付平台：reqData=" + reqData);
        MessagePacket reqPacket = encrypt(reqData);
        MessagePacket respPacket  =portType.payOrder(reqPacket);
        String respData = decrypt(respPacket);
        System.out.println("从统一支付平台接收扣费响应：respData=" + respData);
		
        return getStatus(respData);
        
        /*if ("0".equals(getStatus(respData))){
        	return true;
        }else{
        	return false;
        }*/
	}
	
	private String getStatus(String respData){
		int startPos = respData.indexOf("Status=")+"Status=".length();
		int endPos = respData.indexOf("&", startPos);
		
		
		return respData.substring(startPos, endPos);
	}
	
	private MessagePacket encrypt(String data) throws Exception {
		try {
			String cerFile = "resource/security/ups.cer";
			String pfxFile = "resource/security/jnszt.pfx";
			NetTrans netTrans = new NetTrans("PKCS12", "GBK");
			MessagePacket request = new MessagePacket();
			request.setData(netTrans.encryptMsg(data, cerFile));
			request.setSign(netTrans.signMsg(data, pfxFile, "12345678"));
			return request;
		} catch (Exception e) {
			throw new Exception(e);
		}
	}

	private String decrypt(MessagePacket respPacket) throws Exception {
		try {
			String cerFile = "resource/security/ups.cer";
			String pfxFile = "resource/security/jnszt.pfx";
			NetTrans netTrans = new NetTrans("PKCS12", "GBK");
			
			
			String respData = netTrans.decryptMsg(respPacket.getData(), pfxFile, "12345678");
			if(netTrans.verifyMsg(respPacket.getSign(), respData, cerFile)) {
				return respData;
			} else {
				throw new Exception("UPS返回的数据签名验证失败：data=" + respPacket.getData() + ", sign=" + respPacket.getSign());
			}
		} catch (Exception e) {
			throw new Exception(e);
		}
	}

	@Override
	public boolean refund(DepositTrans trans) throws Exception{
		
		SPWebService spWS = new SPWebService();
		SPWebServicePortType portType =  spWS.getSPWebServiceHttpPort();
		
		int amount=Integer.parseInt(trans.getAmount(),16);
		String orderId = trans.getId()+"";
		String payType = ConfigProperties.getInstance().getValue("epay.payType");
		String merchantId = ConfigProperties.getInstance().getValue("epay.merchantId");
		String busiType = ConfigProperties.getInstance().getValue("epay.busiType");
		SimpleDateFormat sdf  = new SimpleDateFormat("yyyyMMddHHmmss");
		Date current = new Date();
		String date=sdf.format(current); 
		String account = trans.getCellphone();
		
		String reqData = "Amount="+amount+"&Account="+account+"&PayType="+payType+"&Date="+date+"&MerchantId="+merchantId+"&OrderId="+orderId+"&BusiType="+busiType;
		System.out.println("send Refund request to Epay：reqData=" + reqData);
        MessagePacket reqPacket = encrypt(reqData);
        MessagePacket respPacket  =portType.reverseOrder(reqPacket);
        
        String respData = decrypt(respPacket);
        System.out.println("receive Refund response from epay：respData=" + respData);
        if ("0".equals(getStatus(respData))){
        	return true;
        }else{
        	return false;
        }
		
	}
	
}
